package com.jinniulive.platform.module.video;


import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.TweenAnimationRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jinniulive.platform.R;
import com.jinniulive.platform.adapter.VideoListAdapter;
import com.jinniulive.platform.bean.VideoListInfo;
import com.jinniulive.platform.tools.GetInfoUtil;
import com.jinniulive.platform.urls.NetConfig;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ChildVideoFragment extends Fragment {


    private XRecyclerView xRecyclerView;
    private int startIndex = 1;
    private int count = 10;
    List<VideoListInfo.VideoDataBean.MvlstBean> mData;
    private VideoListAdapter mAdapter;
    private String tokenId;
    private int userId;
    private int typeID;
    private boolean isPullDown = false;

    public ChildVideoFragment() {
    }

    public static ChildVideoFragment getChildVideoFragment(int typeID) {
        ChildVideoFragment childVideoFragment = new ChildVideoFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("typeID", typeID);
        childVideoFragment.setArguments(bundle);
        return childVideoFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_child, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Bundle arguments = getArguments();
        typeID = arguments.getInt("typeID");
        tokenId = GetInfoUtil.getInstance().getTokenId(getActivity());
        userId = GetInfoUtil.getInstance().getUserId(getActivity());

        initView(view);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(linearLayoutManager);

        xRecyclerView.setRefreshHeader(new TweenAnimationRefreshLayout(getContext()));
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallBeat);
        xRecyclerView.setLoadingMoreEnabled(true);
        xRecyclerView.setFootViewText("正在加载中", "没有更多了");
        mAdapter = new VideoListAdapter(getContext());
        download();//第一次下载
        xRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new VideoListAdapter.OnItemClickListener() {
            @Override
            public void addOnClickListener(View view, int position) {
                // TODO: 2016/12/12 条目点击
            }
        });

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            //下拉刷新
            public void onRefresh() {
                isPullDown = true;
                startIndex = 1;
                count = 10;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        download2();//下载数据
                    }
                }, 1000);
            }

            //加载更多
            @Override
            public void onLoadMore() {
                Log.i("TAG", "loadmore");
                startIndex += count;
                count = 10;
                isPullDown = false;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        download2();
                    }
                }, 1000);
            }
        });
    }

    private void download() {
        String path = String.format(NetConfig.VIDEOLIST, tokenId, userId, startIndex, count, typeID);
        x.http().post(new RequestParams(path), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                VideoListInfo videoListInfo = gson.fromJson(result, VideoListInfo.class);
                if (videoListInfo.getErrorcode() == 200) {
                    List<VideoListInfo.VideoDataBean.MvlstBean> mvlst = videoListInfo.getVideoData().get(0).getMvlst();
                    mData = mvlst;
                    mAdapter.setmData(mData);
                } else {
                    Toast.makeText(getContext(), "网络错误", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //刷新加载
    private void download2() {
        String path = String.format(NetConfig.VIDEOLIST, tokenId, userId, startIndex, count, typeID);
        Log.i("path_video", path);
        x.http().post(new RequestParams(path), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                VideoListInfo videoListInfo = gson.fromJson(result, VideoListInfo.class);
                if (videoListInfo.getErrorcode() == 200) {
                    List<VideoListInfo.VideoDataBean.MvlstBean> mvlst = videoListInfo.getVideoData().get(0).getMvlst();
                    if (isPullDown) {//下拉刷新
                        mData = mvlst;
                        xRecyclerView.refreshComplete();
                    } else {//上拉加载
                        if (mvlst.get(0) == null) {
                            xRecyclerView.setNoMore(true);
                        } else {
                            mData.addAll(mvlst);
                            xRecyclerView.loadMoreComplete();
                        }
                    }
                } else {
                    Toast.makeText(getContext(), "网络错误", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                xRecyclerView.setNoMore(true);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView(View view) {
        xRecyclerView = (XRecyclerView) view.findViewById(R.id.video_xrv);
    }
}
